package com.ithema.comfig;


import com.ithema.controller.interception.ProjectInterception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


/*
*    有两种方法
*            第一种采取 标明注解@Compnent组建   继承WebMvcConfigurationSupport形式重写addResourceHandlers和addInterceptors方法
*
*
*
*            第二种简化开发    直接在springmvcconfig配置类中实现拦截器,实现 WebMvcConfigurer    注意：侵入式较强，与springconfig强绑定
*
*
* */



//@Configuration// 当采用 第二种简化开发时，此时这个注解注销掉，以免出现异常等
public class SpringMvcSupport extends WebMvcConfigurationSupport {
    @Autowired
    private ProjectInterception projectInterception;//这里设置拦截器对象

    //该方法下的资源路径不被springmvc自动拦截，                可以对静态资源放行   html css等
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
     registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");//将pages目录下的请求指向/pages/下
     registry.addResourceHandler("/css/**").addResourceLocations("/css/");
     registry.addResourceHandler("/js/**").addResourceLocations("/js/");
     registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
    }

    //拦截器  interception
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns(可变参数)方法，可以写多个，   例如: 相应/books指拦截rest风格下对应相应方法
        registry.addInterceptor(projectInterception).addPathPatterns("/books","/books/*");
    }
}
